package com.webank.wedatasphere.qualitis.util;

import com.webank.wedatasphere.qualitis.project.entity.ProjectMenu;

import java.util.List;
import java.util.stream.Collectors;

public class CreateTreeUtils {

    // 创建树形结构
    public static List<ProjectMenu> createTree(List<ProjectMenu> allListVo) {
        List<ProjectMenu> treeList = allListVo.stream().filter(parentNode -> parentNode.getParentId() == 0L)
                .map(node -> getChild(node, allListVo)).collect(Collectors.toList());
        return treeList;
    }

    /**
     * 递归辅助函数
     *
     * @param sql     上级节点
     * @param allList 所有数据
     */
    public static ProjectMenu getChild(ProjectMenu sql, List<ProjectMenu> allList) {
        /**
         * 1、filter：获取子节点pid是否等于id
         * 2、map：递归获取子节点
         * */
        List<ProjectMenu> treeSQL = allList.stream()
                .filter(subNode -> subNode.getParentId().equals(sql.getId()))
                .map(subNode -> getChild(subNode, allList)).collect(Collectors.toList());
        sql.setChildren(treeSQL);
        return sql;
    }

}
